package com.example.yls.demoa;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

   findViewById(R.id.btn_1).setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
            AlertDialog dialog=new AlertDialog.Builder(MainActivity.this)
                    .setTitle("对话框标题")
                    .setIcon(R.mipmap.ic_launcher)
                    .setMessage("你好  我是第一种普通对话框")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(MainActivity.this,"你点击了OK",Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(MainActivity.this,"你点击了NO",Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setNeutralButton("中", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(MainActivity.this,"你点击了中",Toast.LENGTH_SHORT).show();
                        }
                    })
                    .create();
            dialog.show();

       }
   });

      findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
           final String[] items = {"16软1","16软2","16软3","16软4"};
           final AlertDialog dialog=new AlertDialog.Builder(MainActivity.this)
                   .setTitle("请选择班级")
                   .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialogInterface, int i) {
                           Toast.makeText(MainActivity.this,"你选择了"+items[i],Toast.LENGTH_SHORT).show();
                           dialogInterface.dismiss();
                       }
                   })
                   .show();
          }
      });



        findViewById(R.id.btn_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items= {"16软1","16软2","16软3","16软4"};
                AlertDialog dialog=new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请选择班级")
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this,"你选择了"+items[i],Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });

        findViewById(R.id.btn_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items={"农药","代码","吃鸡","飞车"};
                final boolean[] choice = new boolean[items.length];
                final AlertDialog dialog=new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请选择爱好")
                        .setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                choice[i] = b;
                            }
                        })
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                StringBuffer buffer = new StringBuffer();
                                buffer.append("你的爱好包括:");
                                for(int j=0;j<choice.length;j++){
                                    if(choice[j]){
                                        buffer.append(items[j]);
                                    }
                                }
                                Toast.makeText(MainActivity.this,buffer.toString(),Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });

        findViewById(R.id.btn_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText edtName=new EditText(MainActivity.this);
                final AlertDialog dialog=new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请输入姓名")
                        .setView(edtName)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this, "请输入你的姓名"+edtName.getText().toString(), Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });

        findViewById(R.id.btn_6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final View layout= LayoutInflater.from(MainActivity.this).inflate(R.layout.input,null,false);
                final EditText edtName= layout.findViewById(R.id.edtName);
                final EditText edtAge= layout.findViewById(R.id.edtAge);
                final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请输入姓名年龄")
                        .setView(layout)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this,"你的输入姓名是"+edtName.getText().toString()+"年龄是"+edtAge.getText().toString(),Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });
        findViewById(R.id.btn_7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProgressDialog waitting = new ProgressDialog(MainActivity.this);
                        waitting.setTitle("我是一个等待对话框");
                     waitting.setMessage("正在加载中，请耐心等待........");
                     waitting.setIndeterminate(true);
                     waitting.setCancelable(false);
                     waitting.show();
            }
        });
        findViewById(R.id.btn_8).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final int MAX_PROGRESS = 100;
                final ProgressDialog progressDialog =
                        new ProgressDialog(MainActivity.this);
                progressDialog.setProgress(0);
                progressDialog.setTitle("我是一个进度条");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setMax(MAX_PROGRESS);
                progressDialog.show();
    /* 模拟进度增加的过程
     * 新开一个线程，每个100ms，进度增加1
     */
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int progress= 0;
                        while (progress < MAX_PROGRESS){
                            try {
                                Thread.sleep(100);
                                progress++;
                                progressDialog.setProgress(progress);
                            } catch (InterruptedException e){
                                e.printStackTrace();
                            }
                        }
                        // 进度达到最大值后，窗口消失
                        progressDialog.cancel();
                    }
                }).start();
            }
        });


    }
}



